package com.xuan.pmh.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xuan.pmh.R;
import com.xuan.pmh.bean.Paper;
import com.xuan.pmh.bean.Question;
import com.xuan.pmh.item.QuestionItem;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ToastUtil;

/**
 * @author xuan 2014-10-30
 * 
 */
public class WritePaperActivity extends HttpActivity {

	private LinearLayout llQuestions;
	private TextView tvInstructuiions;
	private Button btFinish, btReturn;

	private final int GET_PAPER = 0;
	private final int WRITE_PAPER = 1;
	private QuestionItem questionItem;
	List<Paper> papers;
	List<View> questionView=new ArrayList<View>();
	Paper paper;
	
	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		if (gsonBean.state == FinalValue.FAIL) {
			ToastUtil.show(this, "操作失败");
			return;
		}
		switch (requestCode) {
		case GET_PAPER:
			papers = gson.fromJson(gsonBean.bean, new TypeToken<List<Paper>>() {
			}.getType());
			getPaperShow();
			break;
		case WRITE_PAPER:
			ToastUtil.show(this, "提交成功,转至下一问卷");
			llQuestions.removeAllViews();
			if (gsonBean.state == FinalValue.SUCCESS) {
				getPaperShow();
			}
			break;
		}
	}

	public void getPaperShow() {
		if (papers.size() != 0) {
			
			paper = papers.get(0);
			papers.remove(0);
			questionView.clear();
			if (paper.questions != null) {
				
				for (int i = 0; i < paper.questions.size(); i++) {
					Question q = paper.questions.get(i);
					View child = questionItem.createQuestion("问题" + (i + 1), q);
					questionView.add(child);
					mPrint("add");
					llQuestions.addView(child);
				}
			}
		} else {
			ToastUtil.show(this, "没有未完成的问卷了");
		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_write_paper;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		questionItem = new QuestionItem(inflater);

		llQuestions = (LinearLayout) findViewById(R.id.llQuestions);
		tvInstructuiions = (TextView) findViewById(R.id.tvInstructions);
		btFinish = (Button) mFindViewAndSetOnClick(R.id.btFinish);
		btReturn = (Button) mFindViewAndSetOnClick(R.id.btReturn);

		paramsList.clear();
		addPostRequest("getStudentNotWritePaper", paramsList, GET_PAPER);
		sendPostRequest();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btFinish:
			paramsList.clear();
			int grade = 0;
			for(int i =0;i<questionView.size();i++){
				grade += questionItem.getGrade(questionView.get(i));
			}
			paper.grade=grade;
			paper.questions=null;
			paramsList.put("param", gson.toJson(paper));
			addPostRequest("writePaper", paramsList, WRITE_PAPER);
			sendPostRequest();
			break;
		case R.id.btReturn:
			finish();
			break;
		}
	}

}
